package org.raymond.iworks.study.flowable;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.raymond.iworks.study.flowable.event.TimerEventProcess;

@Slf4j
public class FlowableTest {
    private static RuntimeService runtimeService;
    private static RepositoryService repositoryService;
    private static IdentityService identityService;
    private static TaskService taskService;
    private static HistoryService historyService;

    @BeforeAll
    public static void init(){
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
                .setJdbcUsername("sa")
                .setJdbcPassword("")
                .setJdbcDriver("org.h2.Driver")
                // 设置为true才能触发计时器Timer事件
                .setAsyncExecutorActivate(true)
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        ProcessEngine processEngine = cfg.buildProcessEngine();
        runtimeService = processEngine.getRuntimeService();
        repositoryService = processEngine.getRepositoryService();
        identityService = processEngine.getIdentityService();
        taskService = processEngine.getTaskService();
        historyService = processEngine.getHistoryService();
    }

    //@Test
    public void oneTaskProcess(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("flowable/one-task-process.bpmn20.xml")
                .deploy();
        log.info("Number of process definitions: {}", repositoryService.createProcessDefinitionQuery().count());
        log.info("Number of tasks : {}", taskService.createTaskQuery().count());

        OneTaskProcess oneTaskProcess = OneTaskProcess.instance(runtimeService, taskService);
        String assignee = "kerit";
        oneTaskProcess.startProcess(assignee);
        log.info("Number of tasks after process start : {}", oneTaskProcess.getTasks(assignee));
    }

    //@Test
    public void financialReport(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("flowable/financial-report.bpmn20.xml")
                .deploy();
        log.info("Number of process definitions: {}", repositoryService.createProcessDefinitionQuery().count());
        log.info("Number of tasks : {}", taskService.createTaskQuery().count());

        FinancialReportProcess financialReport = FinancialReportProcess.instance(runtimeService, taskService, historyService);
        String assignee = "demo";
        financialReport.startProcess();
        log.info("Number of tasks after process start : {}", financialReport.getTasks());
    }

    //@Test
    public void financialReportHis(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("flowable/financial-report.bpmn20.xml")
                .deploy();
        FinancialReportProcess financialReport = FinancialReportProcess.instance(runtimeService, taskService, historyService);
        String procId = financialReport.startProcess();
        financialReport.getFirstTask(procId);
    }

    @Test
    public void timer1() throws InterruptedException {
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("flowable/event_timer.bpmn20.xml")
                .deploy();
        TimerEventProcess tiemrEvent = TimerEventProcess.instance(runtimeService, taskService, historyService);
        //String procId = tiemrEvent.startProcess();
        Thread.sleep(1000*60*10);
    }
}

